package com.example.multidatasource.controller;

import com.example.multidatasource.generator.domain.Product;
import com.example.multidatasource.generator.domain.TbProduct;
import com.example.multidatasource.generator.service.ProductService;
import com.example.multidatasource.generator.service.TbProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@RestController
@RequestMapping("/multi")
public class MultiDatasource {
    @Autowired
    private ProductService productService;

    @Autowired
    private TbProductService tbProductService;

    @GetMapping("/hello")
    public String hello() {
        return "Hello, MultiDatasource!";
    }

    @GetMapping("/product")
    public String product() {
        /*从一个库中查出数据*/
        List<Product> products = productService.listByIds(List.of(1));
        Product product = products.get(0);

        //id            //product_name      //product_description
        //pre_price     //now_price         //create_time
        //creator       //update_time       //editor        //yn
        TbProduct tbProduct = new TbProduct();
        tbProduct.setProductName(product.getTitle());
        tbProduct.setProductDescription(product.getDescription());
        tbProduct.setPrePrice(Long.valueOf(product.getOriginalPrice()));
        tbProduct.setNowPrice(Long.valueOf(product.getActualPrice()));
        tbProduct.setCreateTime(product.getCreateTime());
        tbProduct.setCreator("admin");
        tbProduct.setUpdateTime(product.getUpdateTime());
        tbProduct.setEditor("admin");
        tbProduct.setYn(1);

        tbProductService.save(tbProduct);
        return "Hello, MultiDatasource!";
    }

}
